package com.leonzhangxf.bucksdemo.order.domain;

import com.leonzhangxf.bucksdemo.order.domain.value.OrderAmount;
import com.leonzhangxf.bucksdemo.order.domain.value.OrderId;
import com.leonzhangxf.bucksdemo.order.domain.value.OrderStatus;
import org.springframework.util.Assert;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    final OrderRepository orderRepository;

    public OrderServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Transactional
    public void save() {
        OrderId orderId = new OrderId("effb31a763b348d1810ed19e4aa37777");
        OrderAmount orderAmount = new OrderAmount(BigDecimal.ONE, "CNY");
        Order order = Order.newOrder(orderId, orderAmount);

        orderRepository.saveOrder(order);
        // test transaction
        Assert.isTrue(order.getStatus().equals(OrderStatus.PAYING), "状态为待支付");
    }

    @Override
    public void saveOrder(Order order, List<OrderContent> contents) {
        orderRepository.saveOrder(order);
    }
}
